import Emitter from "../libs/tinyemitter";
import { SCREEN_WIDTH, SCREEN_HEIGHT } from "../render";

const atlas = wx.createImage();
atlas.src = "images/Common.png";


const musicOnImg = wx.createImage();
musicOnImg.src = "images/music-on.png";
musicOnImg.width = 256;
musicOnImg.height = 256;

const musicOffImg = wx.createImage();
musicOffImg.src = "images/music-off.png";
musicOffImg.width = 256;
musicOffImg.height = 256;





export default class GameInfo extends Emitter {
  constructor() {
    super();

    /* this.restartBtnArea = {
      startX: SCREEN_WIDTH / 2 - 40,
      startY: SCREEN_HEIGHT / 2 - 100 + 180,
      endX: SCREEN_WIDTH / 2 + 50,
      endY: SCREEN_HEIGHT / 2 - 100 + 255,
    }; */

    const revokeBtnLeft = (SCREEN_WIDTH - GameGlobal.px2dpr(180)) / 3;
    const replayBtnLeft = revokeBtnLeft * 2 + GameGlobal.px2dpr(90);
    const revokeBtnTop = SCREEN_HEIGHT - GameGlobal.px2dpr(100);
    const width = GameGlobal.px2dpr(90);
    const height = GameGlobal.px2dpr(40);
    this.revokeBtnArea = {
      left: revokeBtnLeft,
      top: revokeBtnTop,
      width,
      height,
    };

    this.replayBtnArea = {
      left: replayBtnLeft,
      top: revokeBtnTop,
      width,
      height,
    };

    this.musicBtnArea = {
      left: GameGlobal.px2dpr(20),
      top: GameGlobal.px2dpr(50),
      width: GameGlobal.card.width,
      height: GameGlobal.card.height,
    };


    // 绑定触摸事件
    wx.onTouchStart(this.touchEventHandler.bind(this));

    

  }

  setFont(ctx) {
    ctx.fillStyle = "#ffffff";
    ctx.font = GameGlobal.px2dpr(20) + "px Arial";
  }

  render(ctx) {
    // this.renderGameScore(ctx, GameGlobal.databus.score); // 绘制当前分数
    
    this.renderGameRevoke(ctx);

    if (GameGlobal.musicManager.status === "on") {
      this.drawMusicOnButton(ctx);
    } else {
      this.drawMusicOffButton(ctx);
    }

    // 游戏结束时停止帧循环并显示游戏结束画面
    // if (GameGlobal.databus.isGameOver) {

    //   // this.renderGameOver(ctx, GameGlobal.databus.score); // 绘制游戏结束画面
    // }
  }

  /* renderGameScore(ctx, score) {
    this.setFont(ctx);
    ctx.fillText(score, 10, 30);
  } */
  renderGameRevoke(ctx) {
    /* this.drawGameOverImage(ctx);
    this.drawGameOverText(ctx,'撤回1'); */

    
    
    this.drawRevokeButton(ctx);
  }

  // renderGameOver(ctx, score) {
  //   this.drawGameOverImage(ctx);
  //   this.drawGameOverText(ctx, score);
  //   this.drawRestartButton(ctx);
  // }

  // drawGameOverImage(ctx) {
  //   ctx.drawImage(
  //     atlas,
  //     0,
  //     0,
  //     119,
  //     108,
  //     SCREEN_WIDTH / 2 - 150,
  //     SCREEN_HEIGHT / 2 - 100,
  //     300,
  //     300
  //   );
  // }

  // drawGameOverText(ctx, text) {
  //   this.setFont(ctx);
  //   ctx.fillText(
  //     text,
  //     SCREEN_WIDTH / 2 - 40,
  //     SCREEN_HEIGHT / 2 - 100 + 50
  //   );
  //   /* ctx.fillText(
  //     `得分: ${score}`,
  //     SCREEN_WIDTH / 2 - 40,
  //     SCREEN_HEIGHT / 2 - 100 + 130
  //   ); */
  // }

  // drawRestartButton(ctx, text) {
  //   ctx.drawImage(
  //     atlas,
  //     120,
  //     6,
  //     39,
  //     24,
  //     SCREEN_WIDTH / 2 - 60,
  //     SCREEN_HEIGHT / 2 - 100 + 180,
  //     120,
  //     40
  //   );
  //   ctx.fillText(
  //     "从头开始",
  //     SCREEN_WIDTH / 2 - 40,
  //     SCREEN_HEIGHT / 2 - 100 + 205
  //   );
  // }

  drawMusicOnButton(ctx) {
    // musicImg.src = "images/music-on.png";

    // ctx.clearRect(
    //   this.musicBtnArea.left,
    //   this.musicBtnArea.top,
    //   this.musicBtnArea.width,
    //   this.musicBtnArea.height
    // ); // 清空画布
    ctx.drawImage(
      musicOnImg,
      0,
      0,
      musicOnImg.width,
      musicOnImg.height,
      this.musicBtnArea.left,
      this.musicBtnArea.top,
      this.musicBtnArea.width,
      this.musicBtnArea.height
    );
     
    
  }
  drawMusicOffButton(ctx) {
    ctx.drawImage(
      musicOffImg,
      0,
      0,
      musicOffImg.width,
      musicOffImg.height,
      this.musicBtnArea.left,
      this.musicBtnArea.top,
      this.musicBtnArea.width,
      this.musicBtnArea.height
    );
  }
    drawRevokeButton(ctx, text) {
      let textLeft = 0
    {
      ctx.fillStyle = "#ffffff";
      ctx.font = GameGlobal.px2dpr(40) + "px Arial";
      const txt = `第 ${GameGlobal.databus.GameIndex} 关`;
      ctx.measureText(txt).width;
      textLeft =  (SCREEN_WIDTH - ctx.measureText(txt).width) / 2
      ctx.fillText(txt,textLeft, GameGlobal.px2dpr(80));
    }

    {
      const { left, top, width, height } = this.revokeBtnArea;
      ctx.drawImage(atlas, 120, 6, 39, 24, left, top, width, height);
      this.setFont(ctx);
      ctx.fillText("撤  回", left + GameGlobal.px2dpr(20), top + GameGlobal.px2dpr(25));
    }

    {
      const { left, top, width, height } = this.replayBtnArea;
      ctx.drawImage(atlas, 120, 6, 39, 24, left, top, width, height);
      ctx.fillText("重  玩", left + GameGlobal.px2dpr(20), top + GameGlobal.px2dpr(25));
    }
  }

  touchEventHandler(event) {
    let { clientX, clientY } = event.touches[0]; // 获取触摸点的坐标
    clientX  = GameGlobal.px2dpr(clientX)
    clientY  = GameGlobal.px2dpr(clientY)
    // 当前只有游戏结束时展示了UI，所以只处理游戏结束时的状态
    if (GameGlobal.databus.isGameOver) {
      // 检查触摸是否在按钮区域内
      /* if (
        clientX >= this.restartBtnArea.startX &&
        clientX <= this.restartBtnArea.endX &&
        clientY >= this.restartBtnArea.startY &&
        clientY <= this.restartBtnArea.endY
      ) {
        // 调用重启游戏的回调函数
        this.emit("restart");
      } */
    } else {
      // 检查触摸是否在按钮区域内
      if (
        clientX >= this.revokeBtnArea.left &&
        clientX <= this.revokeBtnArea.left + this.revokeBtnArea.width &&
        clientY >= this.revokeBtnArea.top &&
        clientY <= this.revokeBtnArea.top + this.revokeBtnArea.height
      ) {
        // 调用撤回游戏的回调函数
        this.emit("revoke");
      }

      if (
        clientX >= this.replayBtnArea.left &&
        clientX <= this.replayBtnArea.left + this.replayBtnArea.width &&
        clientY >= this.replayBtnArea.top &&
        clientY <= this.replayBtnArea.top + this.replayBtnArea.height
      ) {
        // 调用撤回游戏的回调函数
        this.emit("replay");
      }

      if (
        clientX >= this.musicBtnArea.left &&
        clientX <= this.musicBtnArea.left + this.musicBtnArea.width &&
        clientY >= this.musicBtnArea.top &&
        clientY <= this.musicBtnArea.top + this.musicBtnArea.height
      ) {

        // 调用撤回游戏的回调函数
        this.emit("music", GameGlobal.musicManager.status);
      }
    }
  }
}
